#!/usr/bin/python3
# -*- coding: utf-8 -*-
import csv
import hmac
import hashlib
import urllib
import urllib.parse
import json
import time
import requests
import base64
import os
import traceback
import datetime
import re
import sys


def get_list():
    file = r"D:\jenkins\workspace\api\jen\scripts\test.csv"  # 路径需修改
    with open(file, "r", encoding="utf-8") as f:
        reader = csv.reader(f)
        next(reader)
        row_list = []
        for row in reader:
            print(row[13] + "，" + row[1] + "毫秒")
            if row[13] not in row_list:
                row_list.append(row[13])
                row_list.append(row[1])
        list2 = []
        num = 0
        while True:
            if num == len(row_list):
                break
            else:
                list2.append(row_list[len(row_list) - (len(row_list) - num)] + "，" + row_list[len(row_list) - (len(
                    row_list) - (num + 1))] + "毫秒")
                num += 2
    return str(list2)


def signature():
    times = int(round(time.time() * 1000))
    # 调试 secret = 'SEC12808f1d15e1e4722ab587c65ff28edaf00962701d3116cc698f1add5e820208'
    # 测试 secret = 'SEC3a0275898d1f366619e0f953e374a1226259deb788c71f3771fadf38c619df6f'
    # 测试三组 secret = 'SECf14b256db35631e40c2db06dd60256d63f3074d05699eb71b2bdcbad61766dce'
    # 雪纳瑞 SECcafc9e77d0342c6c5cb06ea74cb845deb3dd824c1245ab9d48fb29f984e36e13
    # jmeter压测告警 SECdb9a6dd02f0212b8f0f77fb5827fab489abf7e8e09f3672ddf38b9315b3957e9
    secret = 'SECdb9a6dd02f0212b8f0f77fb5827fab489abf7e8e09f3672ddf38b9315b3957e9'
    secret_enc = str(secret).encode('utf-8')
    string_to_sign = '{}\n{}'.format(times, secret)
    string_to_sign_enc = str(string_to_sign).encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote(base64.b64encode(hmac_code))
    return times, sign


def dingtalk_publish(threadNum):
    times, sign = signature()
    row_list = get_list().replace(',', '\n').replace("'", '').replace("[", ' ').replace("]", '')
    # 调试 https://oapi.dingtalk.com/robot/send?access_token=6d9595d480610e650bd7462de5525249c44ffb0c7f47f66346ed44f8ed89bc20
    # 测试 https://oapi.dingtalk.com/robot/send?access_token=a48ee18f5c005ee2c348d4a76a50223ebaa1fd761929533ae972735b10062f40
    # 测试三组 https://oapi.dingtalk.com/robot/send?access_token=8ae81394957488b16a7ad972c6263c2670a4fd4e2daa636f65dcbbfc43e1f216
    # 雪纳瑞 https://oapi.dingtalk.com/robot/send?access_token=3dbc33705b1cba25064515400b2f477165ba815cd9e81e4b2a1c670f2740d65c
    # jmeter压测告警https://oapi.dingtalk.com/robot/send?access_token=e5f621d674a6b3f7d77bf34e559fa97137147901201b5427bd7fbb5521456969
    url = 'https://oapi.dingtalk.com/robot/send?access_token=e5f621d674a6b3f7d77bf34e559fa97137147901201b5427bd7fbb5521456969' + '&timestamp=' + str(
        times) + '&sign=' + sign
    data = {
        "msgtype": "text",
        "text": {
            "content": f"【进行{threadNum}个并发】\n{row_list}"
        },
        "at": {
            "atMobiles": [
                "176xxxx3205",
                "189xxxx8325"
            ],
            "isAtAll": False
        }
    }
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.post(url, data=json.dumps(data), headers=headers)
    result = response.json()
    # print(json.dumps(result, ensure_ascii=False, indent=4))
    print(str(time.strftime("%H:%M:%S")) + "推送成功")


num = sys.argv[1]  # 传递的第一个参数 sys.argv[0]表示脚本名称
dingtalk_publish(num)

# while True:
#     if time.strftime("%H:%M:%S") < "21:00:00":
#         runJmeter(10)
#     if time.strftime("%H:%M:%S") >= "21:00:00":
#         runJmeter(100)
#     time.sleep(60 * 60)  
